#ifndef DBARCH_H
#define DBARCH_H

#include "dbfile.h"
#include "dbident.h"
#include "dbparachute.h"

#define BLOCK_MAX_SIZE  0xFFFF
#define DEFAULT_VALIDATION_BUFFER   0x5000  /// 20KB

#define TIME_SHIFT_SUMMER       -1           /// Summer time UTC+2.00 - default value for Warsaw
#define TIME_SHIFT_WINTER       1           /// Winter time UTC+1.00 - default value for Warsaw



class DBArch
{
	public:
		DBArch();
		virtual ~DBArch();

		i32 SetArchPath ( string path, i32 relative = 1 ) {arch_path = path; g_ArchPath = path; return 1;}
		i32 GetArchPath ( string &path, i32 &relative ) { path = arch_path; return 1;}

		//protected:

		i32 openArchive ( time_t sys_date, i32 writable = 1 );
		i32 closeArchive ( DBIdent *ident );
		i32 createNewFile ( time_t sys_time, time_t ext_time );
		i32 writeBlock ( time_t sys_time, time_t ext_time, u8 type, u32 size, void *data );
		i32 searchBlocks ( time_t time_from, time_t time_to, u8 type, u32 flags, vector<dbf_search_result> &results );
		i32 search ( time_t time_from, time_t time_to, u8 type, u32 flags, vector<dbf_search_result> &results );
		i32 readBlock ( dbf_search_result result, void *data ); // preallcoated buffer
		i32 allocateBlock ( time_t sys_time, time_t ext_time, i32 size, u8 type, dbf_block **block_ptr = 0, dbf_bat **batptr = 0 );
		i32 addBat ( DBIdent *ident );
		i32 setCurrent ( DBIdent *ident );
		i32 writeHeader ( DBIdent *ident );
		i32 validateArch ( DBIdent *ident );

		i32 lookupIdent ( time_t sys_time, u32 &cache );

		i32 llread  ( DBIdent *ident, void *ptr, i32 size, i32 fpointer = -1 ); // low level read
		i32 llwrite ( DBIdent *ident, void *ptr, i32 size, i32 fpointer );
		i32 llsize  ( DBIdent *ident, u32 &size );

		string timeToFName ( time_t sys_date );


		i32 removeIdent ( DBIdent *ident );
		i32 pushToStack ( DBIdent *ident );
		i32 removeOldStack();

		time_t sysClock() { return ( time ( 0 ) + tDiff );}
		time_t extClock() { return ( time ( 0 ) + tDiffExt );}

		u32 isSummerTime(time_t sys_time);          /// return true if it's summer time

//        protected:
		DBIdent *arch_now;
		vector<DBIdent *> cache_read;
		string arch_path;
		time_t tDiff;                   // difference between computer clock and my sysclock
		time_t tDiffExt;                // difference between external clock and my sysclock


	private:
};

#endif // DBARCH_H
